package org.jprogger.myworld.app.auth.service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.SortedMap;
import java.util.TreeMap;

import org.jprogger.myworld.api.secure.DigestUtils;
import org.jprogger.myworld.app.auth.AppAuthConfig;

public class AuthValidationService {

	public boolean isValidAppId(String appId) {
		
		boolean isValidAppId = false;
		String applicationId = AppAuthConfig.APP_ID;
		
		if(applicationId.equalsIgnoreCase(appId)) 
		{
			isValidAppId = true;
		}
		return isValidAppId;
	}
	
	public boolean isValidAuthKey(String viewerId, String authKey) {
		
		boolean isValidKey = false;
		String appId = AppAuthConfig.APP_ID;
		String secretKey = AppAuthConfig.SECRET_KEY;
		StringBuilder builder = new StringBuilder();
		SortedMap<String, Object> paramMap = new TreeMap<String, Object>();
		
		builder.append(appId);
		builder.append("_");
		builder.append(viewerId);
		builder.append("_");
		paramMap.put("param", builder.toString());
		try 
		{
			String validAuthKey = DigestUtils.makeDigest(paramMap, null);
			if (validAuthKey.equalsIgnoreCase(authKey))
			{
				isValidKey = true;
			}
		} 
		catch (UnsupportedEncodingException see) 
		{
			// TODO
		}
		catch (NoSuchAlgorithmException sae) 
		{
			// TODO
		}
		return isValidKey;
	}
}
